package com.jelly.web.modules.system.dept.service.impl;

import com.jelly.common.constant.Constant;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.BuildTreeUtil;
import com.jelly.common.web.tree.Tree;
import com.jelly.config.shiro.token.TokenUtil;
import com.jelly.web.modules.system.dept.dao.DeptMapper;
import com.jelly.web.modules.system.dept.domain.Dept;
import com.jelly.web.modules.system.dept.domain.DeptExample;
import com.jelly.web.modules.system.dept.service.IDeptService;
import com.jelly.web.modules.staff.user.domain.User;
import com.jelly.web.modules.staff.user.domain.UserExample;
import com.jelly.web.modules.staff.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @创建人 ql
 * @创建时间 2018-08-22 15:53
 * @项目名称 jelly
 * @功能描述:部门接口实现
 **/
@Service
public class DeptServiceImpl implements IDeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private IUserService userService;

    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Dept> getDeptList(Dept dept) {
        DeptExample example = new DeptExample();
        DeptExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isNull(dept)){
            if(StringUtils.isNotBlank(dept.getDeptName())){
                criteria.andDeptNameEqualTo(dept.getDeptName());
            }

            if(StringUtils.isNotBlank(dept.getIsState())){
                criteria.andIsStateEqualTo(dept.getIsState());
            }
        }

        example.setOrderByClause("stort asc");

        return deptMapper.selectByExample(example);

    }

    /**
     * 获取所有部门
     * @return
     */
    @Override
    public List<Dept> getDeptList(String isState) {

        DeptExample example = new DeptExample();
        example.createCriteria().andIsStateEqualTo(isState);

        return deptMapper.selectByExample(example);
    }
    /**
     * 通过部门id获取部门信息
     * @return
     */
    @Override
    public Dept getDeptId(String deptId) {
        return deptMapper.selectByPrimaryKey(deptId);
    }
    /**
     * 通过部门id修改部门信息
     * @return
     */
    @Override
    public boolean updateDept(Dept dept) {
        if(StringUtils.isNotBlank(dept.getBaseId())){
            dept.setDeptId(dept.getBaseId());
        }
        dept.setUpdateDate(new Date());
        dept.setUpdateId(TokenUtil.getUserId());
        return deptMapper.updateByPrimaryKeySelective(dept) > 0;
    }
    /**
     * 新增部门信息
     * @return
     */
    @Override
    public boolean addDept(Dept dept) {
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        dept.setAddDate(new Date());
        dept.setAddId(TokenUtil.getUserId());
        dept.setDeptId(id);
        return deptMapper.insertSelective(dept)  > 0;
    }
    /**
     * 删除部门信息
     * @return
     */
    @Override
    public boolean delectDept(String deptId) {
        UserExample example = new UserExample();
        example.createCriteria().andDeptIdEqualTo(deptId);

        List<User> list = userService.getUser(example);

        if(ListUtil.isNotList(list)){
            User user = new User();
            user.setDeptId("");
            List<String> list1 = ListUtil.getFieldValues(list,"userId");
            userService.updateUser(list1,user);
        }

        return deptMapper.deleteByPrimaryKey(deptId) > 0;
    }

    @Override
    public Tree<Dept> getTreeDept() {

        DeptExample example = new DeptExample();

        example.createCriteria().andIsStateEqualTo(Constant.SUCCESS);

        example.setOrderByClause("stort asc");

        List<Dept> list = deptMapper.selectByExample(example);

        List<Tree<Dept>> trees = new ArrayList<Tree<Dept>>();

        if(ListUtil.isNotList(list)){

            for (Dept dept : list){
                Tree<Dept> tree = new Tree<Dept>();
                tree.setId(dept.getDeptId());
                tree.setParentId(dept.getParentId());
                tree.setText(dept.getDeptName());
                if("0".equals(dept.getParentId())){
                    Map map = new HashMap();
                    map.put("opened",true);
                    tree.setState(map);
                }
                trees.add(tree);
            }

        }

        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<Dept> t = BuildTreeUtil.build(trees);

        return t;
    }

}
